<template>
  <div class="app-container">

    <el-card class="box-card">
      <el-form  :rules="rules"  ref="ruleForm" :model="ruleForm"  label-width="150px" v-loading='loading' >
        

        <el-form-item label="类别名称：" prop="displayName">
          <el-input v-model="ruleForm.displayName" />
        </el-form-item>

        <el-form-item label="类别类型："  prop="displayType" >
          <el-select   v-model="ruleForm.displayType" placeholder="请选择">
                      <el-option
                        v-for="item in positionList"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id"
                      >
                      </el-option>
                  </el-select>
        </el-form-item>


        <el-form-item>
            <el-button @click="handleCancel">取消</el-button>
            <el-button :loading='loading' type="primary" @click="handlePublish">保存</el-button>
        </el-form-item>

      </el-form>
    </el-card>

  </div>
</template>



<script>
import api from '@/api/version/category'
import {uploadPath} from '@/api/storage'

import { getToken } from '@/utils/auth'

export default {
  name: 'categoryEdit',
  data() {
    return {
        loading:false,
        uploadPath:uploadPath,
        positionList:[{id:0,name:"版本类型"},{id:1,name:"引擎类型"},{id:2,name:"购买类型"}],
        ruleForm: {
          displayName:'',
          displayType:'',
        },
       rules:{
          displayName: [{ required: true, message: '请输入类型名称', trigger: 'blur' }],
          displayType:[{ required: true, message: '请选择类型类别', trigger: 'change' }],
       },

    }
  },
  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  },
  created() {
   
  },
  mounted() {
    this.init()
  },
  methods: {
  
  init(){
        if (this.$route.query.id == null) {
          return
        }
        const id = this.$route.query.id
        api.read({id:id}).then(res=>{
            this.ruleForm.displayName=res.data.data.displayName
            this.ruleForm.displayType=res.data.data.displayType
        })
      },

    handleCancel: function() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.push({ path: '/category/list' })
    },
    handlePublish() {

       this.$refs["ruleForm"].validate((valid) => {
          if (valid) {
              this.Create(this.ruleForm)
          } else {
            this.$message({
              message: '请检查数据必填项',
              type: 'warning'
            });
            return false
          }
        })
    },

  Create(e) {
      this.loading = true
      api.create(e).then(res => {
          if(res.data.errno==0){
            this.loading = false
            this.$message({
            message: '新增成功',
            type: 'success'
          });
             this.$router.push({ path: '/category/list' })
          }
      }).catch((e) => {
        this.loading = false
        this.$message({
              message: e.data,
              type: 'warning'
            });
      })
    },

  }
}
</script>
<style lang="scss" scoped>


  
</style>